package com.yuke.cloud.service.uac.controller.frontend;

import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.uac.dto.AccLogMainDto;
import com.yuke.cloud.service.uac.entity.AccLog;
import com.yuke.cloud.service.uac.service.AccLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import com.yuke.cloud.common.core.support.BaseController;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by wg on 2018/09/24.
*/
@Api(value = "WEB - AccLogController", tags = "处理AccLog相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/acc/log")
public class AccLogController extends BaseController {
    @Resource
    private AccLogService accLogService;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "accLog", value = "该数据记录的POJO") @RequestBody AccLog accLog) {
        accLogService.save(accLog);
        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        accLogService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "accLog", value = "需要修改的内容") @RequestBody AccLog accLog) {
        accLogService.update(accLog);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<AccLog> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        AccLog accLog = accLogService.findById(id);
        return WrapMapper.ok(accLog);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<AccLog>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<AccLog> list = accLogService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    /**
     * 查询日志列表.
     *
     * @param accLogQueryDtoPage the acc log query dto page
     *
     * @return the wrapper
     */
    @PostMapping(value = "/queryListWithPage")
    @ApiOperation(httpMethod = "POST", value = "查询日志列表")
    public Wrapper queryLogListWithPage(@ApiParam(name = "uacLogQueryDtoPage", value = "日志查询条件") @RequestBody AccLogMainDto accLogQueryDtoPage) {
        logger.info("查询日志处理列表 accLogQueryDtoPage={}", accLogQueryDtoPage);
        PageInfo pageInfo = accLogService.queryLogListWithPage(accLogQueryDtoPage);
        return WrapMapper.ok(pageInfo);
    }

    /**
     * 分页查询用户操作日志列表.
     *
     * @param log the log
     *
     * @return the wrapper
     */
    @PostMapping(value = "/queryUserLogListWithPage")
    @ApiOperation(httpMethod = "POST", value = "分页查询用户操作日志列表")
    public Wrapper<PageInfo<AccLog>> queryUserLogListWithPage(@ApiParam(name = "user", value = "用户信息") @RequestBody AccLog log) {

        logger.info("分页查询用户操作日志列表");
        PageHelper.startPage(log.getPageNum(), log.getPageSize());
//        List<AccLog> list = accLogService.selectUserLogListByUserId(getLoginAuthDto().getUserId());
        List<AccLog> list = accLogService.selectUserLogListByUserId(log.getOperatorId());
        PageInfo<AccLog> pageInfo = new PageInfo<>(list);
        return WrapMapper.ok(pageInfo);
    }
}
